package com.pearlsoft.webapp.core.dataset;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pearlsoft.commons.utils.JsonUtil;

/**
 * 
 * Result
 * 
 * @Creator haitingr
 * @Create-DateTime 2011-10-25
 */
public class Result {

	protected String statusCode = "200";

	protected String message = "success";
	
	protected boolean success = true;
	protected List<Record> records;
	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public byte[] toJson() throws IOException {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("status", this.statusCode);
		values.put("message", this.message);
		values.put("success", isSuccess());
		if(records!=null && records.size()>0 ){
			List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
			for( Record record:records ){
				recordList.add(record.toMap());
			}
			values.put("records", recordList);
		}else{
			values.put("records", null);
		}
		return JsonUtil.toJson(values);
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	/**
	 * @return the records
	 */
	public List<Record> getRecords() {
		if(records==null){
			records = new ArrayList<Record>();
		}
		return records;
	}

	/**
	 * @param records the records to set
	 */
	public void setRecords(List<Record> records) {
		this.records = records;
	}



}
